define(['masonry'], function(require, exports, module) {
    function bindUploadPhoto() {
        require.async('photo', function(a) {
            a.init({
                clickObj: $('.js-show-up')
            });
        });
    }

    function doMasonry() {
        var container = $('#photo-list');
        container.imagesLoaded(function() {
            container.masonry({
                columnWidth: 2,
                itemSelector: '.thumbnail',
                animated: true
            });
            $('#photo-list .thumbnail').fadeIn();
        });
    }

    function bindEditPhoto() {
        $('.js-edit-btn').on('click', function() {
            var src = $(this).closest('.thumbnail').find('.js-user-photo').attr('src');
            var id = $(this).closest('.thumbnail').attr('photoId');
            var titleObj = $(this).closest('.thumbnail').find('.js-title');
            var descObj = $(this).closest('.thumbnail').find('.js-desc');
            var title = titleObj ? titleObj.html() : '';
            var desc = descObj ? descObj.html() : '';
            require.async('photo', function(a) {
                a.showEdit(src, '', id, title, desc);
            });
        });
    }

    function bindDelPhoto() {
        $('.js-del-btn').on('click', function() {
            if (!confirm('确定要删除吗？')) {
                return;
            }
            var id = $(this).closest('.thumbnail').attr('photoId');
            $.ajax({
                url: '/Ajax/User/delPhoto',
                type: 'post',
                data: {id:id},
                dataType: 'json',
                success: function(d) {
                    if (d.error) {
                        alert(d.data);
                    } else {
                        window.location.reload();
                    }
                }
            });
        });
    }

    exports.init = function() {
        bindUploadPhoto();
        doMasonry();
        bindEditPhoto();
        bindDelPhoto();
    }
});